import java.util.Arrays;

public class FindLUSlength2 {

    public static void main(String[] args) {
        FindLUSlength2 findLUSlength2 = new FindLUSlength2();
        String[] strings = {"j","j","dyzf","ogk","ogk","lfn","ypmhwx","ypmhwx","m","m","ak","ivivzoncju","ivivzoncju","wmybi","wmybi","dyzfjg","dyzfjg"};
        System.out.println(findLUSlength2.findLUSlength(strings));
    }

    public int findLUSlength(String[] strs) {
        Arrays.sort(strs, (a, b) -> {
            if (a.length() != b.length()) return b.length() - a.length();
            return a.compareTo(b);
        });
        if (!strs[0].equals(strs[1])) return strs[0].length();
        String s = strs[0];
        for (int i = 1; i < strs.length; i++) {
            if (i != strs.length - 1 && (strs[i].equals(strs[i + 1]) || strs[i].equals(strs[i - 1]))) continue;
            if (i == strs.length - 1 && strs[i].equals(strs[i - 1])) continue;
            int p1 = 0;
            int p2 = 0;
            while (p1 < strs[i].length() && p2 < s.length()) {
                if (strs[i].charAt(p1) == s.charAt(p2)) ++p1;
                ++p2;
            }
            if (p1 != strs[i].length()) return strs[i].length();
        }
        return -1;
    }
}
